<?php
/**
 * Created by 杜鹏
 * createTime  :2015/9/11 0011下午 4:50
 * description :
 */
namespace Home\Controller;



class MemberController extends CheckController
{
    public function myteam()
    {
        $openid = session('openid');
        $res = D('TreestructView');
        $myinfo = $res->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
        $list = $res->query('SELECT
                                s.id,
                                s.lvl,
                                s.lft,
                                s.rgt,
                                s.pid,
                                m.name from onethink_treemember m,
                                onethink_treestruct s
                            WHERE
                                s.id = m.id
                            AND s.lft > '.$myinfo['lft'].'
                            AND s.rgt < '.$myinfo['rgt'].'
                            AND s.lvl < '.($myinfo['lvl']+4).'
                            AND m.isshow=1
                            ORDER BY
                                s.lft');
        $count = array('one'=>0,'two'=>0,'three'=>0,'pid'=>$myinfo['id']);
        foreach ($list as $k => $v) {
            switch ($v['lvl']) {
                case $myinfo['lvl']+1:
                    $count['one']++;
                    break;
                case $myinfo['lvl']+2:
                    $count['two']++;
                    break;
                case $myinfo['lvl']+3:
                    $count['three']++;
                    break;
            }
        }
        $this->assign('count',$count);
        $this->display();
    }

    public function myteamdetail()
    {
        $pid = I('pid');
        $lvl = I('lvl');
        if (!$_GET['pid'] || !$_GET['lvl']) {
            $this->error('请勿进行非法操作!');
        }
        $res = D('TreestructView');
        $myinfo = $res->where(array('id'=>$pid))->find();
        $list = M()->query('SELECT
                            s.id,
                            s.lvl,
                            s.lft,
                            s.rgt,
                            s.pid,
                            m.name,
                            m.tel
                        FROM
                            onethink_treemember m,
                            onethink_treestruct s
                        WHERE
                            s.id = m.id
                        AND s.lft > '.$myinfo['lft'].'
                        AND s.rgt < '.$myinfo['rgt'].'
                        AND s.lvl = '.($myinfo['lvl']+$lvl).'
                        ORDER BY
                            s.lft ');
        $this->assign('_list',$list);
        $this->display();
    }

    public function test()
    {
        p($_SESSION);
    }

    /**
     * 健康档案显示
     */
    public function health()
    {
        $openid = session('openid');
        $treemember = M('treemember');
        $healthypic = M('healthypic');
        $info = $treemember->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
        $list = $healthypic->where(array('u_id'=>$info['id']))->select();
        $this->assign('_list',$list);
        $this->display();
    }

    /**
     * 我的积分
     */
    public function myintegral()
    {
        $changeintegral = M('integralchange');
        $treemember = M('treemember');
        $openid = session('openid');
        $per_info = $treemember->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
        $list = $changeintegral->where(array('uid'=>$per_info['id']))->order(array('id'=>'desc'))->limit(10)->select();
        foreach ($list as $k => $v) {
            switch ($v['type']) {
                case 1:
                    $list[$k]['type'] = '购物';
                    break;
                case 2:
                    $list[$k]['type'] = '提现';
                    break;
                case 3:
                    $list[$k]['type'] = '其他';
                    break;
                case 4:
                    $list[$k]['type'] = '推荐加分';
                    break;
                case 5:
                    $list[$k]['type'] = '再消费';
                    break;
                case 6:
                    $list[$k]['type'] = '下级消费';
                    break;
                case 7:
                    $list[$k]['type'] = '区域消费';
                    break;
                default:
                    $list[$k]['type'] = '其他';
                    break;
            }
        }
        $this->assign('_list',$list);
        $this->assign('info',$per_info);
        $this->display();
    }
    /**
     * 添加新会员
     */
    public function member_add()
    {
        if (IS_POST) {
            $treemember = D('treemember');
            $data = $_POST;
            $openid = session('openid');
            unset($data['user_add_1']);
            unset($data['user_add_2']);
            unset($data['user_add_3']);
            $data['add_1'] = $_POST['user_add_1'];
            $data['add_2'] = $_POST['user_add_2'];
            $data['add_3'] = $_POST['user_add_3'];
            $data['isshow'] = 2;
            $data['createtime'] = time();
            if ($openid === null) {
                $this->ajaxReturn(array('info'=>'微信认证失败，请重新打开!','reg'=>2),'json');
            }
            if (!$treemember->create($data)) {
                $this->ajaxReturn(array('info'=>$treemember->getError(),'reg'=>2),'json');
            }
            M()->startTrans();
            $treemember_res = $treemember->where("openid_1='$openid' or openid_2='$openid' or openid_3='$openid'")->find();
            $parentId = $treemember_res['id'];
            $treestruct = M('Treestruct');
            $treestruct_res = $treestruct->where("id='$parentId'")->find();
            $add_int = D('Treemember')->addintegral($parentId);
            if ($add_int  === false) {
                M()->rollback();
                $this->ajaxReturn(array('info'=>'推荐人加分失败，请联系管理员解决!','reg'=>2),'json');
            }
            $level = $treestruct_res['lvl'];
            $left = $treestruct_res['lft'];
            //将所有左值大于当前分类左值的分类左右值加2
            $res = $treestruct->where("lft>$left")->setInc('lft',2);
            if ($res === false) {
                M()->rollback();
                $this->ajaxReturn(array('info'=>'添加失败，请联系管理员!','reg'=>2),'json');
            }
            $res = $treestruct->where("lft>$left")->setInc('rgt',2);
            if ($res === false) {
                M()->rollback();
                $this->ajaxReturn(array('info'=>'添加失败，请联系管理员!','reg'=>2),'json');
            }
            $res = $treestruct->where("rgt>$left AND lft <= $left")->setInc('rgt',2);
            if ($res === false) {
                M()->rollback();
                $this->ajaxReturn(array('info'=>'添加失败，请联系管理员!','reg'=>2),'json');
            }
            $treestruct_data['lft'] = $left + 1;
            $treestruct_data['rgt'] = $treestruct_data['lft'] + 1;
            $treestruct_data['pid'] = $parentId;
            $treestruct_data['lvl'] = $level + 1;
            $res = $treestruct->data($treestruct_data)->add();
            if (!$res) {
                M()->rollback();
            }
            $data['uid'] = $res;
            $data['id'] = $res;
            $res = $treemember->data($data)->add();
            if (!$res) {
                M()->rollback();
                $this->ajaxReturn(array('info'=>'手机号已经注册过了!','reg'=>2),'json');
            }
            /*提交事务*/
            M()->commit();
            $this->ajaxReturn(array('info'=>'推荐成功','reg'=>1),'json');
        } else {
            $this->display();
        }
    }
}